﻿using System;
using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_126 : BaseProblem
    {
        public override object GetResult()
        {
            const int max = 100000;
            const int ans = 1000;




            var dct = new Dictionary<long, long>();

            long res = 0;
            bool ex = false;

            long maxA = (max - 2)/4;

            for (long a = 1; a <= maxA; a++)
            {
                long maxB = (max - 2*a)/(2*a + 2);
                if (maxB > a)
                    maxB = a;
                for (long b = 1; b <= maxB; b++)
                {
                    long maxC = (max - 2*a*b)/(2*a + 2*b);
                    if (maxC > b)
                        maxC = b;
                    for (long c = 1; c <= maxC; c++)
                    {
                        for (long  n = 1; n <= long.MaxValue; n++)
                        {
                            long rs = 2*(2*(a + b + c + n - 2)*(n - 1) + a*b + a*c + b*c);
                            if (rs > max)
                                break;
                            if (!dct.ContainsKey(rs))
                                dct.Add(rs, 0);
                            dct[rs]++;
                        }
                    }
                }
            }

            for (long i = 2; i <= max; i +=2)
            {
                if (!dct.ContainsKey(i)) continue;
                if (dct[i] == ans)
                    return i;
            }
            return -1;
        }

        private static long GetCountForN(long value)
        {
            if (value % 2 != 0) return 0;

            long res = 0;
            long maxN = (long)(Math.Sqrt(value - 6)/2 + 1);

            for (long n = 1; n <= maxN; n++ )
            {
                long minA = (long) (1 - n + Math.Sqrt(((n + 1)*(n - 1) + value)/(double)3));
                long maxA = (value/2-1+2*n-2*n*n)/2/n;
                for (long a = minA; a <= maxA; a++)
                {
                    for (long b = 1; b <= a; b++)
                    {
                        for (long c = 1; c <= b; c++)
                        {
                            if (2*(2*(a+b+c+n-2)*(n-1)+a*b+a*c+b*c) != value) continue;
                            res++;
                        }
                    }
                }

            }
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"The minimum number of cubes to cover every visible face on a cuboid measuring 3 x 2 x 1 is twenty-two.


If we then add a second layer to this solid it would require forty-six cubes to cover every visible face, the third layer would require seventy-eight cubes, and the fourth layer would require one-hundred and eighteen cubes to cover every visible face.

However, the first layer on a cuboid measuring 5 x 1 x 1 also requires twenty-two cubes; similarly the first layer on cuboids measuring 5 x 3 x 1, 7 x 2 x 1, and 11 x 1 x 1 all contain forty-six cubes.

We shall define C(n) to represent the number of cuboids that contain n cubes in one of its layers. So C(22) = 2, C(46) = 4, C(78) = 5, and C(118) = 8.

It turns out that 154 is the least value of n for which C(n) = 10.

Find the least value of n for which C(n) = 1000.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 18522;
            }
        }

    }
}
